<?php


namespace app\controller\admin;


use app\BaseController;
use app\models\goods\ExchangeOderModel;
use app\models\goods\ProductModel;
use think\facade\Db;

class ProductController extends BaseController
{
    /**
     * 获取商品列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList()
    {
        $name = $this->request->param('goods_name', '', 'trim');
        $where = [];
        $where[] = ['status', '=', 0];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $list = ProductModel::where($where)
            ->order('id', 'desc')
            ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
            ->toArray();
        return $this->success($list);
    }

    /**
     * 添加，编辑
     * @return \think\response\Json
     */
    public function save()
    {
        try {
            $id = $this->request->param('id');
            $params = $this->request->only(['goods_name', 'goods_img', 'price', 'stock', 'intro']);
            if (!$id) {
                $model = new ProductModel();
                $model->save($params);
            } else {
                $model = ProductModel::find($id);
                $model->save($params);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id');
            $model = ProductModel::find($id);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            $model->status = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 兑换订单
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getOrderList()
    {
        $name = $this->request->param('user_name', '', 'trim');
        $where = [];
        if ($name) {
            $where[] = ['user_name', 'like', '%' . $name . '%'];
        }
        $order_sn = $this->request->param('order_sn', '', 'trim');
        if ($order_sn) {
            $where[] = ['order_sn', 'like', '%' . $order_sn . '%'];
        }
        $list = ExchangeOderModel::where($where)
            ->order('id', 'desc')
            ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
            ->toArray();
        return $this->success($list);
    }

    public function sendGoods()
    {
        try {
            $id = $this->request->param('id');
            $model = ExchangeOderModel::find($id);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            $deliveryCompany = $this->request->param('delivery_company');
            $deliveryNo = $this->request->param('delivery_no');
            $model->delivery_company = $deliveryCompany;
            $model->delivery_no = $deliveryNo;
            $model->order_status = 2;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}